package com.yuyang.wifi.utils;

import java.math.BigDecimal;

/**
 * Created by yang on 2017/11/23.
 * <p>
 * 钱单位转换的工具类
 */

public class FormatMoneyUtil {

    /**
     * //得到的样式  100.00 (元)
     * <p>
     * 将分为单位的数额转换为元
     *
     * @param amount
     * @return
     */
    public static String pennyChangeDollar(Long amount) {
        if (amount == null) return "";
        try {
            if (!amount.toString().matches("\\-?[0-9]+")) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        int flag = 0;
        String amString = amount.toString();
        if (amString.charAt(0) == '-') {
            flag = 1;
            amString = amString.substring(1);
        }
        StringBuffer result = new StringBuffer();
        if (amString.length() == 1) {
            result.append("0.0").append(amString);
        } else if (amString.length() == 2) {
            result.append("0.").append(amString);
        } else {
            String intString = amString.substring(0, amString.length() - 2);
            for (int i = 1; i <= intString.length(); i++) {
                if ((i - 1) % 3 == 0 && i != 1) {
                    result.append("");
                }
                result.append(intString.substring(intString.length() - i, intString.length() - i + 1));
            }
            result.reverse().append(".").append(amString.substring(amString.length() - 2));
        }
        if (flag == 1) {
            String num = result.toString();
            String substring = num.substring(num.length() - 2, num.length());
            String lastNum = num.substring(num.length() - 1, num.length());
            if (!lastNum.equals("0")) {
                return result.toString();
            } else {
                if (substring.equals("00")) {
                    result.substring(0, num.length() - 2);
                } else if (substring.contains("0")) {
                    result.substring(0, num.length() - 1);
                }
            }
            return result.toString();
        } else {
            String num = result.toString();
            String substring = num.substring(num.length() - 2, num.length());
            String lastNum = num.substring(num.length() - 1, num.length());
            if (!lastNum.equals("0")) {
                return result.toString();
            } else {
                if (substring.equals("00")) {
                    String substring2 = result.substring(0, num.length() - 3);
                    return substring2;
                } else if (substring.contains("0")) {
                    String substring1 = result.substring(0, num.length() - 1);
                    return substring1;
                }
            }
            return result.toString();
        }
    }


    /**
     * //得到的样式  123,456,100.00 (元)
     * <p>
     * 将分为单位的数额转换为元
     *
     * @param amount
     * @return
     */
    public static String pennyChangeDollarThree(Long amount) {
        try {
            if (!amount.toString().matches("\\-?[0-9]+")) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        int flag = 0;
        String amString = amount.toString();
        if (amString.charAt(0) == '-') {
            flag = 1;
            amString = amString.substring(1);
        }
        StringBuffer result = new StringBuffer();
        if (amString.length() == 1) {
            result.append("0.0").append(amString);
        } else if (amString.length() == 2) {
            result.append("0.").append(amString);
        } else {
            String intString = amString.substring(0, amString.length() - 2);
            for (int i = 1; i <= intString.length(); i++) {
                if ((i - 1) % 3 == 0 && i != 1) {
                    result.append("");
                }
                result.append(intString.substring(intString.length() - i, intString.length() - i + 1));
            }
            result.reverse().append(".").append(amString.substring(amString.length() - 2));
        }
        if (flag == 1) {
            return "-" + result.toString();
        } else {
            return result.toString();
        }
    }

    //用户输入金额转化成分数   不损失精度
    public static String double2String(double d) {
        BigDecimal bg = new BigDecimal(d * 100);
        double doubleValue = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return String.valueOf((int) doubleValue);
    }


}
